﻿using System;
using System.ServiceModel;
using System.Threading;
using Me.Practice.ElevatorEngine.Infrastructure;
using Me.Practice.ElevatorEngine.Server.Services;

namespace Me.Practice.ElevatorEngine.Server
{
	class Program
	{
		private static ReportServiceHost _sReportingServiceHost;
		
		private static void InitializeServiceHost()
		{	
			_sReportingServiceHost = new ReportServiceHost(typeof(ReportService), new Uri[] { });
			
			_sReportingServiceHost.Opening += delegate {
                Console.Clear();
				Console.WriteLine("Starting Simulation engine service host . . .");
			};
			
			_sReportingServiceHost.Opened += delegate {
                Console.Clear();
				Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Simulation service hosted on {0}", _sReportingServiceHost.BaseAddresses[0]);
				Console.ResetColor();
				Console.WriteLine();
			};
			
			_sReportingServiceHost.Closing += delegate {
                Console.Clear();
                Console.WriteLine("Stopping Simulation engine WCF host . . .");
			};
			
			_sReportingServiceHost.Closed += delegate {
                Console.Clear();
                Console.WriteLine("Simulation engine WCF-host is stopped.");
			};
			
			_sReportingServiceHost.Faulted += delegate {
                Console.Clear();
				Console.ForegroundColor = ConsoleColor.Red;				
				Console.WriteLine("[FAIL]: Somethig went wrong while WCF service host open operation.");
				Console.ResetColor();
			};
			
			try
			{
				_sReportingServiceHost.Open();
			}
			catch(Exception e)
			{
				Console.ForegroundColor = ConsoleColor.Red;
				Console.WriteLine(e.Message);
				Console.ResetColor();
			}			
		}
		
		public static void Main(string[] args)
		{
            var header = new string[]
		                     {
		                         "/////////////////////////////////////////////////////////////",
		                         "//                                                         //",
		                         "// Elevator Emulator Server                                //",
		                         "//                                                         //",
		                         "// Author: Sergey L. Pravodelov                            //",
		                         "// Email : slpravodelov.job@gmail.com                      //",
		                         "// Cell Phone : +7 (929) 041 0330                          //",
		                         "//                                                         //",
		                         "/////////////////////////////////////////////////////////////"
		                     };

			Console.WriteLine(string.Join("\n", header));

            // Prepare WCF service host
			InitializeServiceHost(); 
            
            // Activate building context
		    var serverContextThread = new Thread(BuildingContext.ActivateEngine);
            serverContextThread.Start();

            Console.WriteLine(string.Join("\n", header));
		    Console.WriteLine();

            Console.Write("Press any key to stop server . . . ");
			Console.ReadKey(true);
            try
            {
                if (_sReportingServiceHost.State == CommunicationState.Opened)
                    _sReportingServiceHost.Close();

                BuildingContext.DeactivateEngine();

                Console.WriteLine();
                Console.Write("Server state: {0}", serverContextThread.ThreadState);
                Console.WriteLine();
            }
            catch(Exception e)
            {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine();
                Console.Write(e.Message);
                Console.WriteLine();
                Console.ResetColor();
            }

		    Console.Write("Press any key to exit . . . ");
			Console.ReadKey(true);
		}
	}
}